package com.housediyadmin.view
{
	
	/**
	 * 
	 * @author Bo Zhang
	 * 2010/11/16
	 */	
	import com.adobe.cairngorm.process.ProcessEvent;
	import com.housediyadmin.events.RemoveRelateGoodsEvent;
	import com.housediyadmin.events.brandEvents.AddBrandEvent;
	import com.housediyadmin.events.brandEvents.DeleteBrandByIDEvent;
	import com.housediyadmin.events.brandEvents.EditBrandEvent;
	import com.housediyadmin.events.brandEvents.GetBrandEvent;
	import com.housediyadmin.events.bubbleEvents.CheckBrandIDBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.CheckFittingIDBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.DeleteBrandBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.DeleteFittingBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.EditBrandBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.EditFittingBubbleEvt;
	import com.housediyadmin.events.fittingEvents.AddFittingEvent;
	import com.housediyadmin.events.fittingEvents.DeleteFittingByIDEvent;
	import com.housediyadmin.events.fittingEvents.GetFittingEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByBrandIDEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByDictFlagEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByFittingIDEvent;
	import com.housediyadmin.model.BrandModel;
	import com.housediyadmin.model.FittingModel;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.ListTypeModel;
	import com.housediyadmin.vo.Brand;
	import com.housediyadmin.vo.Fitting;
	import com.housediyadmin.vo.RelateGoods;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.controls.DateField;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.DragSource;
	import mx.core.IFlexDisplayObject;
	import mx.core.IUIComponent;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.events.DragEvent;
	import mx.events.FlexEvent;
	import mx.events.IndexChangedEvent;
	import mx.events.ListEvent;
	import mx.events.Request;
	import mx.formatters.DateFormatter;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.Label;
	import spark.components.List;
	import spark.events.IndexChangeEvent;

	
	public class FittingAdminMediator extends Mediator
	{
		[Inject]
		public var view:FittingAdminView;
		
		[Inject]
		public var XMLModel:ListTypeModel;
		
		[Inject]
		public var fittingModel:FittingModel;
		
		[Inject]
		public var brandModel:BrandModel;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		public var TypeArray:ArrayList = new ArrayList(["门","窗","地板"]);
		
		public var selectedDictId:int = 0;
		
		private var fileRefs: FileReference = new FileReference();
		public var urlrequest: URLRequest = new URLRequest("http://localhost/housediyadmin/RoomUploadService.aspx");
		public var picfile:FileReference = new FileReference();
		
		
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.fittingList, "dataProvider", fittingModel, "fittingsArray");
			dispatch(new GetFittingEvent(GetFittingEvent.GET_ALL_FITTINGS));
			
			BindingUtils.bindProperty(view.brandList, "dataProvider", brandModel, "brandArray");
			dispatch(new GetBrandEvent(GetBrandEvent.GET_ALL_BRANDS));
			
			
			//fitting
			view.addFittingBtn.addEventListener(MouseEvent.CLICK, addFittingView);
			view.deleteSeveralFittingBtn.addEventListener(MouseEvent.CLICK, deleteSeveralFitting);	
			view.addEventListener(CheckFittingIDBubbleEvt.ADD_TO_FITTINGIDS, addIdToFittingIdsHandler);
			
			view.addEventListener(DeleteFittingBubbleEvt.DELETE_A_FITTING, deleteFittingEve);
			view.addEventListener(EditFittingBubbleEvt.EDIT_A_FITTING, editFittingView);
			
			
			
			//brand	
			view.addBrandBtn.addEventListener(MouseEvent.CLICK, addBrandView);
			view.addEventListener(DeleteBrandBubbleEvt.DELETE_A_BRAND, deleteBrandEve);
			
			view.deleteSeveralBrandBtn.addEventListener(MouseEvent.CLICK, deleteSeveralBrand);
			view.addEventListener(CheckBrandIDBubbleEvt.ADD_TO_BRANDIDS, addIdToBrandIdsHandler);
			
			view.addEventListener(EditBrandBubbleEvt.EDIT_A_BRAND, editBrandView);

			fileRefs.addEventListener(Event.SELECT, fileSelectHandler);
			
			
		}
		
		private function changeState(event:IndexChangeEvent):void
		{
			switch (event.newIndex)
			{
				case 0:
					view.secondTypeList.dataProvider = XMLModel.doorXMLList;			
					break;
				case 1:
					view.secondTypeList.dataProvider = XMLModel.windowXMLList;
					break;
				case 2:
					view.secondTypeList.dataProvider = XMLModel.floorXMLList;
					break;
			}
			
			selectedDictId = event.newIndex + 1;
			
			view.secondTypeList.addEventListener(IndexChangeEvent.CHANGE, changeAllGoods);
			
		}
		
		private function deleteFittingEve(event:DeleteFittingBubbleEvt):void
		{
			Alert.yesLabel = "是";
			Alert.noLabel = "否";
			Alert.cancelLabel = "取消";
			Alert.show("删除相应的配套信息吗？", "", 3, view, deleteFittingAlert);
			fittingModel.fittingIds = new ArrayCollection();
			fittingModel.fittingIds.addItem(event.fittingId);
		}
		
		private function deleteBrandEve(event:DeleteBrandBubbleEvt):void
		{
			Alert.yesLabel = "是";
			Alert.noLabel = "否";
			Alert.cancelLabel = "取消";
			Alert.show("删除相应的配套信息吗？", "", 3, view, deleteBrandAlert);
			brandModel.brandIds = new ArrayCollection();
			brandModel.brandIds.addItem(event.brandId);
		}
		
		private function deleteBrandAlert(event:CloseEvent):void
		{
			if(event.detail == Alert.YES)
			{
				var brandArr:Array = new Array;			
				brandArr.push(brandModel.brandIds[0]);
				dispatch(new DeleteBrandByIDEvent(DeleteBrandByIDEvent.DELETE_BRAND_BY_ID, brandArr));
			}
		}
		
		private function deleteFittingAlert(event:CloseEvent):void
		{
			if(event.detail == Alert.YES)
			{
				var fittingArr:Array = new Array;			
				fittingArr.push(fittingModel.fittingIds[0]);
				dispatch(new DeleteFittingByIDEvent(DeleteFittingByIDEvent.DELETE_FITTING_BY_ID, fittingArr));
			}
		}
		
		private function changeAllGoods(event:IndexChangeEvent):void
		{
			dispatch(new GetGoodsByDictFlagEvent(GetGoodsByDictFlagEvent.GET_GOODS_BY_DICTFLAG, selectedDictId, event.newIndex));
			view.selectedGoodsList.dataProvider = goodsModel.selectedRelateGoodsArray;
			view.allGoodslist.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
			view.addEventListener(RemoveRelateGoodsEvent.REMOVE_RELATEGOODS, removeRelateGoodsHandler);
			view.selectedGoodsList.addEventListener(DragEvent.DRAG_ENTER, dragEnterHandler);		
			view.selectedGoodsList.addEventListener(DragEvent.DRAG_DROP, dragDropHandler);
			
			
		}
		
		private function dragEnterHandler(event:DragEvent):void
		{
			var selectedRelateGoods:RelateGoods = event.dragSource.dataForFormat(event.dragSource.formats[0]) as RelateGoods;		
			var couldDrop:Boolean = true;
			
			for each (var relateGoods:RelateGoods in goodsModel.selectedRelateGoodsArray)
			{
				if (relateGoods.relateGoodsName == selectedRelateGoods.relateGoodsName)
				{
					couldDrop = false;
					break;	
				}
			}
			
			if (couldDrop == true && event.dragSource.hasFormat("moveRelateGoods"))
			{
				DragManager.acceptDragDrop(IUIComponent(event.target));
			}	
		}
		
		private function dragDropHandler(event:DragEvent):void
		{
			goodsModel.selectedRelateGoodsArray.addItem(event.dragSource.dataForFormat(event.dragSource.formats[0]) as RelateGoods);
		}
		
		private function removeRelateGoodsHandler(event:RemoveRelateGoodsEvent):void
		{
			goodsModel.selectedRelateGoodsArray.removeItemAt(goodsModel.selectedRelateGoodsArray.getItemIndex(event.relateGoods));
		}
		
		private function startDragHandler(event:MouseEvent):void
		{
			if (view.allGoodslist.selectedItem != null)
			{
				var ds:DragSource = new DragSource();
				ds.addData((view.allGoodslist.selectedItem as RelateGoods), "moveRelateGoods");
				
				var imageProxy:Label = new Label();
				imageProxy.text = (view.allGoodslist.selectedItem as RelateGoods).relateGoodsName;
				
				DragManager.doDrag(view.allGoodslist, ds, event, imageProxy);
			}
			else
			{
				Alert.show("请选择需要添加的商品");
			}	
		}
		
		private function deleteSeveralFitting(event:MouseEvent):void
		{
			if (fittingModel.fittingIds.length == 0)
			{
				Alert.show("请选择删除的配套信息");
			}
			else
			{
				var fittingArr:Array = fittingModel.selectedFittingArr;
				
				for each(var fitting:Fitting in fittingArr)
				Alert.yesLabel = "是";
				Alert.noLabel = "否";
				Alert.cancelLabel = "取消";
				Alert.show("删除相应的配套信息", "", 1|2|8, view, deleteFittingHandler);
			}	
		}
		
		private function deleteFittingHandler(event:CloseEvent):void
		{
			if (event.detail == Alert.YES)
			{
				var fittingArr:Array = new Array;
				var i:int = 0;
				for (i; i < fittingModel.fittingIds.length; i++)
				{
					fittingArr.push(fittingModel.fittingIds[i]);
				}
				dispatch(new DeleteFittingByIDEvent(DeleteFittingByIDEvent.DELETE_FITTING_BY_ID, fittingArr));
			}	
		}
		
		private function addIdToFittingIdsHandler(event:CheckFittingIDBubbleEvt):void
		{
			if (fittingModel.fittingIds.contains(event.fittingId))
			{
				var index:int = fittingModel.fittingIds.getItemIndex(event.fittingId);
				fittingModel.fittingIds.removeItemAt(index);
			}
			else
			{
				fittingModel.fittingIds.addItem(event.fittingId);
			}
		}
		
		private function addFittingView(event:MouseEvent):void
		{		
			view.currentState = "addFitting";
			view.addBrandBtn.enabled = true;
			view.okBtn.addEventListener(MouseEvent.CLICK, addFittingEve);
			view.cancelBtn.addEventListener(MouseEvent.CLICK, cancelEve);
			view.addFittingBtn.enabled = false;
			BindingUtils.bindProperty(view.allGoodslist, "dataProvider", goodsModel, "relateGoodsArray");
			
			BindingUtils.bindProperty(view.selectedGoodsList, "dataProvider", goodsModel, "selectedRelateGoodsArray");

			view.topTypeList.dataProvider = TypeArray;
			view.topTypeList.addEventListener(IndexChangeEvent.CHANGE, changeState);
		}
		
		private function editFittingView(event:EditFittingBubbleEvt):void
		{
			view.currentState = "editFitting";
			fittingModel.selectedFitting = event.fitting;
			BindingUtils.bindProperty(view.allGoodslist, "dataProvider", goodsModel, "relateGoodsArray");		
			BindingUtils.bindProperty(view.selectedGoodsList, "dataProvider", goodsModel, "selectedRelateGoodsArray");
			view.topTypeList.dataProvider = TypeArray;
			view.topTypeList.addEventListener(IndexChangeEvent.CHANGE, changeState);
			BindingUtils.bindProperty(view.addFittingName, "text", fittingModel.selectedFitting, "fittingName");
			BindingUtils.bindProperty(view.addFittingDesc, "text", fittingModel.selectedFitting, "fittingDesc");
			dispatch(new GetGoodsByFittingIDEvent(GetGoodsByFittingIDEvent.GET_GOODS_BY_FITTINGID, fittingModel.selectedFitting.fittingID));
//			view.selectedGoodsList.dataProvider = goodsModel.s;
	        
		}
		
		private function editBrandView(event:EditBrandBubbleEvt):void
		{
			view.currentState = "editBrand";
			brandModel.selectedBrand = event.brand;
			BindingUtils.bindProperty(view.addBrandName, "text", brandModel.selectedBrand, "brandName");
			BindingUtils.bindProperty(view.addBrandDesc, "text", brandModel.selectedBrand, "brandIntroduce");
			view.img.source = "http://localhost/housediyadmin/upload/"+ brandModel.selectedBrand.brandPicSrc;
			
			view.brandCancelBtn.addEventListener(MouseEvent.CLICK, cancelEve);
			view.brandOkBtn.addEventListener(MouseEvent.CLICK, editBrandEve);
			view.selectBrandBtn.addEventListener(MouseEvent.CLICK, selectPic);
			view.addBrandBtn.enabled = false;
		}
		
		private function addFittingEve(event:MouseEvent):void
		{
			var fitting:Fitting = new Fitting();
			fitting.fittingName = view.addFittingName.text;
			fitting.fittingDesc = view.addFittingDesc.text;
			fitting.fittingCreateDate = new Date();
			fitting.fittingEditDate = new Date();
			fitting.fittingEditor = "zb";
			fitting.fittingCreator = "zb";

			var goodsIds:Array = new Array();
			for (var i:int = 0; i < goodsModel.selectedRelateGoodsArray.length; i++)
			{
				goodsIds.push((goodsModel.selectedRelateGoodsArray[i] as RelateGoods).relateGoodsID);
			}
			dispatch(new AddFittingEvent(AddFittingEvent.ADD_FITTING, fitting, goodsIds));	
			fittingModel = new FittingModel;
			goodsModel = new GoodsModel;
			view.addFittingName.text = null;
			view.addFittingDesc.text = null;
			view.topTypeList.dataProvider = new ArrayList();
			view.secondTypeList.dataProvider = new ArrayList();
			view.currentState = "normal";
			view.addFittingBtn.enabled = true;
		}
		
		private function cancelEve(event:MouseEvent):void
		{
			view.currentState = "normal";
			view.addFittingBtn.enabled = true;
			view.addBrandBtn.enabled = true;
			goodsModel = new GoodsModel;
			fittingModel = new FittingModel;
			brandModel = new BrandModel;
		}
		
		private function addBrandEve(event:MouseEvent):void
		{
			var brand:Brand = new Brand();
			if (view.img.source != null)
			{
				fileRefs.upload(urlrequest);
				brand.brandPicSrc = picfile.name;				
			}
			else
			{
				brand.brandPicSrc = null;
			}
			brand.brandName = view.addBrandName.text;
			brand.brandIntroduce = view.addBrandDesc.text;
			brand.brandCreateDate = new Date();
			brand.brandEditDate = new Date();
			brand.brandCreator = "zb";
			brand.brandEditor = "zb";
			dispatch(new AddBrandEvent(AddBrandEvent.ADD_BRAND, brand));
			brandModel = new BrandModel;
			view.currentState = "normal";
			view.addBrandBtn.enabled = true;
		}
		
		private function editBrandEve(event:MouseEvent):void
		{
			var brand:Brand = brandModel.selectedBrand;
			if (view.img.source != null)
			{
				fileRefs.upload(urlrequest);
				if (brand.brandPicSrc == null)
				{
					brand.brandPicSrc = picfile.name;	
				}				
			}
			else
			{
				brand.brandPicSrc = null;
			}
			brand.brandName = view.addBrandName.text;
			brand.brandPicSrc = picfile.name;
			brand.brandIntroduce = view.addBrandDesc.text;
			brand.brandCreateDate = new Date();
			brand.brandEditDate = new Date();
			brand.brandCreator = "zb";
			brand.brandEditor = "zb";
			dispatch(new EditBrandEvent(EditBrandEvent.EDIT_BRAND, brand));
			brandModel = new BrandModel;
			view.currentState = "normal";
			view.addBrandBtn.enabled = true;
		}
		
		private function addIdToBrandIdsHandler(event:CheckBrandIDBubbleEvt):void
		{
			if (brandModel.brandIds.contains(event.brandId))
			{
				var index:int = brandModel.brandIds.getItemIndex(event.brandId);
				brandModel.brandIds.removeItemAt(index);
			}
			else
			{
				brandModel.brandIds.addItem(event.brandId);
			}
		}
		
		private function deleteSeveralBrand(event:MouseEvent):void
		{
			if (brandModel.brandIds.length == 0)
			{
				Alert.show("请选择删除的品牌信息");
			}
			else
			{
				var brandArr:Array = brandModel.selectedBrandArr;
				
				for each(var brand:Brand in brandArr)
				Alert.yesLabel = "是";
				Alert.noLabel = "否";
				Alert.cancelLabel = "取消";
				Alert.show("删除相应的品牌信息", "", 1|2|8, view, deleteBrandHandler);
			}	
		}
		
		private function deleteBrandHandler(event:CloseEvent):void
		{
			if (event.detail == Alert.YES)
			{
				var brandArr:Array = new Array;
				var i:int = 0;
				for (i; i < brandModel.brandIds.length; i++)
				{
					brandArr.push(brandModel.brandIds[i]);
				}
				dispatch(new DeleteBrandByIDEvent(DeleteBrandByIDEvent.DELETE_BRAND_BY_ID, brandArr));
			}	
		}
		
		private function selectPic(event:MouseEvent):void
		{
			fileRefs.browse([new FileFilter("Images (*.jpg, *.dae, *.gif, *.png)", "*.jpg;*.dae;*.gif;*.png"), new FileFilter("所有文件(*.*)", "*.*")]);
		}
		
		private function fileSelectHandler(event: Event):void
		{
			fileRefs = FileReference(event.target);
			picfile = fileRefs;
			picfile.addEventListener(Event.COMPLETE, completeHandler);
			picfile.load();
		}
		
		private function completeHandler(e: Event): void				  
		{	
			view.img.source = e.target.data;				  
		}
		
		private function addBrandView(event:MouseEvent):void
		{
			view.currentState ="addBrand";
			view.addFittingBtn.enabled = true;
			view.brandCancelBtn.addEventListener(MouseEvent.CLICK, cancelEve);
			view.brandOkBtn.addEventListener(MouseEvent.CLICK, addBrandEve);
			view.selectBrandBtn.addEventListener(MouseEvent.CLICK, selectPic);
			view.addBrandBtn.enabled = false;
			view.img.source = null;
		}
	}
}